package com.mach.platform.sprider.dev;

import com.mach.platform.sprider.Conf;
import com.mach.platform.util.SqlHelper;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;

@Slf4j
public abstract class GerenalStatusDev extends DataHelper implements StatusGetter {

    protected String devName;

    protected SqlHelper sqls;

    public GerenalStatusDev(String ip, Integer port, Integer devID, String devName) {
        super(ip,port,devID);

        this.sqls  = Conf.getSqlHelper();

        this.devName = devName;
    }

    @Override
    public void run(){

        long preSec = 0;
        long nowSec = 0;
        while(true){

            Integer excepCode = 2;

            try {


                DateTime dateTime = new DateTime();
                int fyear = dateTime.getYear();
                int fmonth = dateTime.getMonthOfYear();
                int fday = dateTime.getDayOfMonth();
                int fhour = dateTime.getHourOfDay();
                int fmin = dateTime.getMinuteOfHour();
                int fsec = dateTime.getSecondOfMinute();

                Integer status = this.getStatus();

                //debug
//                status = 2;

                if(excepCode.equals(status)){
                    this.sqls.updateAsync("insert ignore into data_cvt_warn_detail (dev_id,code,fdatetime," +
                            "fyear, fmonth, fday, fhour, fmin) value " +
                            "( " +
                            "'"+this.devName+"',"+
                            23 + ","+
                            "date_format(concat("+fyear+",'-',"+fmonth+",'-',"+fday+",' ',"+fhour+",':',"+fmin+",':',"+fsec+")," +
                            "'%Y-%m-%d %H:%i:%s'),"
                                    +fyear+","+fmonth+","+fday+","+fhour+","+fmin+
                            ")");
                }

                this.sqls.updateAsync("replace into data_cur (dev_id,td_status,update_time) value " +
                        "( " +
                        "'"+this.devName+"',"+
                        status + ","+
                        "date_format(concat("+fyear+",'-',"+fmonth+",'-',"+fday+",' ',"+fhour+",':',"+fmin+",':',"+fsec+")," +
                        "'%Y-%m-%d %H:%i:%s')" +
                        ")");

//                Thread.sleep(1000*60);

            } catch (Exception e) {
                e.printStackTrace();
            }

            preSec = nowSec;


        }


    }

    abstract public Integer getStatus();
}
